package com.ruoyi.common.utils.es;

import com.ruoyi.common.utils.SecurityUtils;
//import com.ruoyi.operationLog.domain.OperationLog;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class ElasticsearchUtils {
    private RestHighLevelClient client;

    private ElasticsearchRestTemplate elasticsearchTemplate;


    @Autowired
    public ElasticsearchUtils(RestHighLevelClient client) {
        this.client = client;
    }



    /**
     * 关闭ES客户端连接
     */
    public void close() throws IOException {
        client.close();
    }

    /**
     * 删除索引
     * @param index 索引名称
     * @return AcknowledgedResponse 删除成功返回AcknowledgedResponse对象
     */
    public AcknowledgedResponse deleteIndex(String index) throws IOException {
        return client.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT);
    }

    /**
     * 索引文档
     * @param index 索引名称
     * @param id 文档id
     * @param json 文档内容(json格式)
     * @return String 返回索引的文档id
     */
    public String indexDocument(String index, String id, String json) throws IOException {
        IndexRequest request = new IndexRequest(index).id(id).source(json, XContentType.JSON);
        return client.index(request, RequestOptions.DEFAULT).getId();
    }

    /**
     * 批量索引文档
     * @param index 索引名称
     * @param ids 文档id列表
     * @param jsons 文档内容列表(json格式)
     * @return BulkResponse 返回批量操作的响应结果
     */
    public BulkResponse bulkIndexDocuments(String index, List<String> ids, List<String> jsons) throws IOException {
        BulkRequest request = new BulkRequest(index);
        for (int i = 0; i < ids.size(); i++) {
            request.add(new IndexRequest().id(ids.get(i)).source(jsons.get(i), XContentType.JSON));
        }
        return client.bulk(request, RequestOptions.DEFAULT);
    }

    /**
     * 删除文档
     * @param index 索引名称
     * @param id 文档id
     */
    public void deleteDocument(String index, String id) throws IOException {
        DeleteRequest request = new DeleteRequest(index, id);
        client.delete(request, RequestOptions.DEFAULT);
    }

    /**
     * 搜索文档
     * @param index 索引名称
     * @param field 搜索字段名
     * @param value 搜索关键字
     * @param listener 异步请求监听器
     */
    public void searchDocuments(String index, String field, String value, ActionListener<SearchResponse> listener) {
        SearchRequest request = new SearchRequest(index);
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchQuery(field, value));
        builder.sort(SortBuilders.fieldSort("timestamp").order(SortOrder.DESC));
        builder.size(10);
        builder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        request.source(builder);
        client.searchAsync(request, RequestOptions.DEFAULT, listener);
    }
}